package com.app.controller.backstage;

import com.app.common.Result;
import com.app.mapper.GoodsExchangeMapper;
import com.app.model.AdminUserModel;
import com.app.model.GoodsExchangeModel;
import com.app.model.GoodsModel;
import com.app.model.StudyModel;
import com.app.service.AdminUserService;
import com.app.service.GoodsAdminService;
import com.app.service.GoodsService;
import com.app.service.StudyAdminService;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

@Controller
@RestController
@RequestMapping("/admin/goods")
public class GoodsAdminController {
    @Autowired
    private GoodsAdminService goodsAdminService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsExchangeMapper goodsExchangeMapper;
    @Autowired
    private AdminUserService adminUserService;

    //分类列表
    @GetMapping("/selectTypeList")
    public Result<Object> selectTypeList() {
        return Result.success(goodsService.selectTypeList());
    }

    //增加和编辑
    @PostMapping("/insertAndUpdateGoods")
    public Result<Object> insertAndUpdateGoods(@RequestBody GoodsModel goods,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getVillageId() != 0) {
            goods.setVillageId(user.getVillageId());
        } else {
            goods.setVillageId(0);
        }
        if (goods.getIntegral() == 0) {
            return Result.error(500, "积分不得小于1");
        }
        if (goods.getId() == 0) {
            goodsAdminService.insert(goods);
        }
        goodsAdminService.updateById(goods);
        return Result.success("操作成功");
    }

    //查看商品详情
    @GetMapping("/selectGoodsInfo")
    public Result<Object> selectGoodsInfo(Integer id) {
        return Result.success(goodsAdminService.selectGoodsInfo(id));
    }

    //删除
    @GetMapping("/deleteGoodsById")
    public Result<Object> deleteGoodsById(Integer id) {
        if (id != null && id != 0) {
            goodsAdminService.deleteGoodsById(id);
        }
        return Result.success("删除成功");
    }

    //查询商品列表
    @GetMapping("/selectGoodsList")
    public Result<Object> selectGoodsList(Integer villageId,Integer page, Integer limit, String name, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        if (user.getVillageId() == 0 && villageId != null) {
            user.setVillageId(villageId);
        }
        return Result.success(goodsAdminService.selectGoodsList(page, limit, name, user.getVillageId()));
    }

    //查询商品标题
    @GetMapping("/selectGoodsTitle")
    public Result<Object> selectGoodsTitle(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(goodsAdminService.selectGoodsTitle(user.getVillageId()));
    }

    //订单管理
    @GetMapping("/selectOrderList")
    public Result<Object> selectOrderList(Integer page, Integer limit, String keyword, Integer id,HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        String userId = TokenUtils.nothingness(token);
        if (userId == null) {
            return Result.error(405, "token不存在");
        }

        AdminUserModel user = adminUserService.getUserById(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(goodsAdminService.selectOrderList(page, limit, keyword, id,user.getVillageId()));
    }

    //核销
    @PostMapping("/WriteOff")
    public Result<Object> WriteOff(@RequestBody GoodsExchangeModel model) throws ParseException {
        GoodsExchangeModel model2 = goodsExchangeMapper.selectById(model.getId());
        if (model2.getStatus() != 0) {
            if (model2.getStatus() == 1) {
                return Result.error(405, "该订单已核销");
            }
            if (model2.getStatus() == 2) {
                return Result.error(405, "该订单已拒绝");
            }
        }
        GoodsExchangeModel model1 = goodsAdminService.WriteOff(model.getId(), model.getStatus(), model.getExchangeTime());
        return Result.success(model1);
    }

    //状态修改
    @GetMapping("/updateStatus")
    public Result<Object> updateStatus(Integer id, Integer status) {
        GoodsModel goodsModel = goodsAdminService.selectGoodsById(id);
        if (goodsModel.getInventory() == 0) {
            return Result.error(405, "库存为0，不能上架");
        }
        GoodsModel model = new GoodsModel();
        model.setId(id);
        model.setStatus(status);
        goodsAdminService.updateById(model);
        return Result.success("操作成功");
    }
}
